package ren.colorful.learnJava.nio;

import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class UseBuffer {
	public static void main(String[] args) {
		
		try {
			RandomAccessFile aFile = new RandomAccessFile("a.txt", "rw");
//			FileInputStream aFile = new FileInputStream("a.txt");
//			FileInputStream aFile = new FileInputStream("E:\\myFiles\\workspace\\learnJava\\src\\main\\java\\a.txt");
			FileChannel inChannel = aFile.getChannel();
			
			ByteBuffer buf = ByteBuffer.allocate(48);
			
			int bytesRead = inChannel.read(buf);
			
			while(bytesRead != -1){
				buf.flip();
				
				while(buf.hasRemaining()){
					System.out.println((char)buf.get());
				}
				
				buf.clear();
				bytesRead = inChannel.read(buf);
			}
			aFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
